require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function($, common, ready, template, Collection, Model, dialog) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'user/master/shopkeeper',
                condition: {
                    limit: 10,
                    offset: 0
                },
                model: {
                    tag_name: 'tr',
                    url: _global.url.api + 'user/master/shopkeeper',
                    event: {
                        '#btn-reject:click': 'reject',
                        '#btn-pass:click': 'pass'
                    },
                    reject: function(e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            title: '填写拒绝原因',
                            width: 400,
                            content: template('add-tpl', _this.data),
                            ok: function() {
                                var description = $('#description').val();
                                $.ajax({
                                    url: _global.url.api + 'user/master/',
                                    type: 'put',
                                    data: {
                                        'master_id': _this.data.id,
                                        'status': 3,
                                        'role': 3,
                                        'description': description
                                    },
                                    success: function(res) {
                                        if(res.code == 0) {
                                            _this.data.status = '<span class="label label-danger">已拒绝</span>';
                                            _this.create_element();
                                        }
                                    },
                                    error: function() {
                                        Alert({msg: '通讯错误', type: 'error'});
                                    }
                                });
                            }
                        }).show();
                    },
                    pass: function(e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '确定要审批吗?',
                            ok: function() {
                                $.ajax({
                                    url: _global.url.api + 'user/master/',
                                    type: 'put',
                                    data: {
                                        'master_id': _this.data.id,
                                        'status': 2,
                                        'role': 3
                                    },
                                    success: function(res) {
                                        if (res.code != 0) {
                                            Alert({msg: res.msg, type: 'error'});
                                        }
                                        if(res.code == 0) {
                                            _this.data.status = '<span class="label label-success">已审核</span>';
                                            _this.create_element();
                                        }
                                    },
                                    error: function() {
                                        Alert({msg: '通讯错误', type: 'error'});
                                    }
                                });
                            },
                            cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
        }
    };

    new index();

});